package com.homepage.service;

import com.homepage.domain.HeadlineImage;
import com.homepage.repository.HeadlineImageRepository;
import com.homepage.service.dto.HeadlineImageDTO;
import com.homepage.service.mapper.HeadlineImageMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link HeadlineImage}.
 */
@Service
@Transactional
public class HeadlineImageService {

    private final Logger log = LoggerFactory.getLogger(HeadlineImageService.class);

    private final HeadlineImageRepository headlineImageRepository;

    private final HeadlineImageMapper headlineImageMapper;

    public HeadlineImageService(HeadlineImageRepository headlineImageRepository, HeadlineImageMapper headlineImageMapper) {
        this.headlineImageRepository = headlineImageRepository;
        this.headlineImageMapper = headlineImageMapper;
    }

    /**
     * Save a headlineImage.
     *
     * @param headlineImageDTO the entity to save.
     * @return the persisted entity.
     */
    public HeadlineImageDTO save(HeadlineImageDTO headlineImageDTO) {
        log.debug("Request to save HeadlineImage : {}", headlineImageDTO);
        HeadlineImage headlineImage = headlineImageMapper.toEntity(headlineImageDTO);
        Optional<HeadlineImage> headlineImageInDB = headlineImageRepository.findHeadlineImageByHeadlineId(headlineImageDTO.getHeadlineId());
        headlineImageInDB.ifPresent(data -> headlineImage.setId(data.getId()));

        HeadlineImage result = headlineImageRepository.save(headlineImage);

        return headlineImageMapper.toDto(result);
    }

    /**
     * Partially update a headlineImage.
     *
     * @param headlineImageDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<HeadlineImageDTO> partialUpdate(HeadlineImageDTO headlineImageDTO) {
        log.debug("Request to partially update HeadlineImage : {}", headlineImageDTO);

        return headlineImageRepository
            .findById(headlineImageDTO.getId())
            .map(existingHeadlineImage -> {
                headlineImageMapper.partialUpdate(existingHeadlineImage, headlineImageDTO);

                return existingHeadlineImage;
            })
            .map(headlineImageRepository::save)
            .map(headlineImageMapper::toDto);
    }

    /**
     * Get all the headlineImages.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<HeadlineImageDTO> findAll() {
        log.debug("Request to get all HeadlineImages");
        return headlineImageRepository.findAll().stream().map(headlineImageMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one headlineImage by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<HeadlineImageDTO> findOne(Long id) {
        log.debug("Request to get HeadlineImage : {}", id);
        return headlineImageRepository.findById(id).map(headlineImageMapper::toDto);
    }


    @Transactional(readOnly = true)
    public Optional<HeadlineImageDTO> findOneByHeadlineId(Long id) {
        log.debug("Request to get HeadlineImage : {}", id);
        return headlineImageRepository.findHeadlineImageByHeadlineId(id).map(headlineImageMapper::toDto);
    }

    /**
     * Delete the headlineImage by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete HeadlineImage : {}", id);
        headlineImageRepository.deleteById(id);
    }
}
